home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 21 / CU Amiga Magazine's Super CD-ROM 21 (1998)(EMAP Images)(GB)[!][issue 1998-04].iso / CUCD / Utilities / DTConvert / convert.c next >
C/C++ Source or Header  |  1998-01-06  |  32KB  |  917 lines

  1.  
  2. /*
  3. **
  4. **  $VER: convert.c 1.5 (6.1.98)
  5. **  datatypes.library/Examples/DTConvert
  6. **
  7. **  Converts file into another format using datatypes
  8. **
  9. **  converters for the various datatype classes
  10. **
  11. **  Written 1996-1998 by Roland 'Gizzy' Mainz
  12. **
  13. */
  14.  
  15. /* project includes */
  16. #include "DTConvert.h"
  17. #include "convert.h"
  18.  
  19. /* local prototypes */
  20. static                 BOOL           ConvertDataType( Object *, struct DataType *, STRPTR );
  21. static                 BOOL           ConvertPicture( Object *, struct DataType *, STRPTR );
  22. static                 BOOL           ConvertAnimation( Object *, struct DataType *, STRPTR );
  23. static                 BOOL           ConvertSound( Object *, struct DataType *, STRPTR );
  24. static DISPATCHERFLAGS ULONG          WriteAnimAndSoundDispatch( REGA0 struct IClass *, REGA2 Object *, REGA1 Msg );
  25.  
  26.  
  27. void Convert( STRPTR srcname, STRPTR datatypename, BOOL iff, STRPTR destname )
  28. {
  29.     struct DataType *destdtn = NULL;
  30.  
  31.     if( datatypename )
  32.     {
  33.       destdtn = ObtainDataTypeA( DTST_RAM, (APTR)datatypename, NULL );
  34.     }
  35.  
  36.     if( srcname && destname )
  37.     {
  38.       if( destdtn || (iff && (!datatypename)) )
  39.       {
  40.         Object *srcdto;
  41.         ULONG   groupid = ((destdtn)?(destdtn -> dtn_Header -> dth_GroupID):(0UL));
  42.         BOOL    retry   = TRUE;
  43.  
  44.         Message( "loading source data \"%s\"\n", srcname );
  45.  
  46. retry:
  47.         if( srcdto = NewDTObject( (APTR)srcname, XTAG( destdtn, DTA_GroupID ), groupid,
  48.                                                  TAG_DONE ) )
  49.         {
  50.           if( ConvertDataType( srcdto, destdtn, destname ) )
  51.           {
  52.             Message( "file successfully converted\n" );
  53.           }
  54.  
  55.           DisposeDTObject( srcdto );
  56.         }
  57.         else
  58.         {
  59.           if( retry )
  60.           {
  61.             retry = FALSE;
  62.  
  63.             /* Kluge that conversion between GID_ANIMATION and GID_MOVIE datatypes gets possible */
  64.             switch( groupid )
  65.             {
  66.               case GID_ANIMATION: groupid = GID_MOVIE;     goto retry;
  67.               case GID_MOVIE:     groupid = GID_ANIMATION; goto retry;
  68.             }
  69.           }
  70.  
  71.           Message( "can't create source object\n" );
  72.         }
  73.  
  74.         ReleaseDataType( destdtn );
  75.       }
  76.       else
  77.       {
  78.         Message( "can' open destination datatype\n" );
  79.       }
  80.     }
  81.     else
  82.     {
  83.       SetIoErr( ERROR_REQUIRED_ARG_MISSING );
  84.     }
  85. }
  86.  
  87.  
  88. static
  89. BOOL ConvertDataType( Object *srcdto, struct DataType *destdtn, STRPTR destfile )
  90. {
  91.     BOOL success = FALSE;
  92.     LONG error   = 0L;
  93.  
  94.     if( srcdto && destfile )
  95.     {
  96.       struct DataType *srcdtn;
  97.  
  98.       (void)GetDTAttrs( srcdto, DTA_DataType, (&srcdtn), TAG_DONE );
  99.  
  100.       Message( "converting \"%s\" %s into \"%s\" %s\n",
  101.               (srcdtn  -> dtn_Header -> dth_Name),
  102.               GetDTString( (srcdtn -> dtn_Header -> dth_GroupID) ),
  103.               ((destdtn)?(destdtn -> dtn_Header -> dth_Name):("IFF")),
  104.               ((destdtn)?GetDTString( (destdtn -> dtn_Header -> dth_GroupID) ):("IFF")) );
  105.  
  106.       switch( srcdtn -> dtn_Header -> dth_GroupID )
  107.       {
  108.         case GID_SYSTEM:
  109.         {
  110.             /* There is currently no system.datatype base class defined */
  111.             error = ERROR_ACTION_NOT_KNOWN;
  112.         }
  113.             break;
  114.  
  115.         case GID_TEXT:
  116.         {
  117.             error = ERROR_NOT_IMPLEMENTED;
  118.         }
  119.             break;
  120.  
  121.         case GID_DOCUMENT:
  122.         {
  123.             /* There is currently no document.datatype base class defined */
  124.             error = ERROR_ACTION_NOT_KNOWN;
  125.         }
  126.             break;
  127.  
  128.         case GID_SOUND:
  129.         {
  130.             success = ConvertSound( srcdto, destdtn, destfile );
  131.             error = IoErr();
  132.         }
  133.             break;
  134.  
  135.         case GID_INSTRUMENT:
  136.         {
  137.             /* There is currently no instrument.datatype base class defined */
  138.             error = ERROR_ACTION_NOT_KNOWN;
  139.         }
  140.             break;
  141.  
  142.         case GID_MUSIC:
  143.         {
  144.             /* There is currently no music.datatype base class defined */
  145.             error = ERROR_ACTION_NOT_KNOWN;
  146.         }
  147.             break;
  148.  
  149.         case GID_PICTURE:
  150.         {
  151.             success = ConvertPicture( srcdto, destdtn, destfile );
  152.             error = IoErr();
  153.         }
  154.             break;
  155.  
  156.         case GID_ANIMATION:
  157.         case GID_MOVIE:
  158.         {
  159.             success = ConvertAnimation( srcdto, destdtn, destfile );
  160.             error = IoErr();
  161.         }
  162.             break;
  163.  
  164.         default:
  165.         {
  166.             error = ERROR_ACTION_NOT_KNOWN;
  167.         }
  168.             break;
  169.       }
  170.     }
  171.  
  172.     SetIoErr( error );
  173.  
  174.     return( success );
  175. }
  176.  
  177.  
  178. static
  179. BOOL ConvertPicture( Object *srcdto, struct DataType *destdtn, STRPTR destfile )
  180. {
  181.     BOOL success = FALSE;
  182.     LONG error   = 0L;
  183.  
  184.     Message( "picture converter\n" );
  185.  
  186.     if( srcdto && destfile )
  187.     {
  188.       Object          *destdto;
  189.       struct DataType *srcdtn;
  190.       struct BitMap   *bm;
  191.  
  192.       /* Some (ugly) picture.datatype needs a DTM_PROCLAYOUT before
  193.        * allowing to access their bitmaps
  194.        */
  195.       (void)GetDTAttrs( srcdto, PDTA_BitMap, (&bm), TAG_DONE );
  196.  
  197.       if( bm == NULL )
  198.       {
  199.         struct gpLayout  gpl;
  200.  
  201.         Message( "source requires layouting...\n" );
  202.  
  203.         /* "Layout" picture (for those picture classes which need this,
  204.          * __NOT__ recommened)
  205.          */
  206.         SetDTAttrs( srcdto, NULL, NULL, PDTA_Remap, FALSE, TAG_DONE );
  207.  
  208.         gpl . MethodID    = DTM_PROCLAYOUT;
  209.         gpl . gpl_GInfo   = NULL;
  210.         gpl . gpl_Initial = 1L;
  211.  
  212.         DoDTMethodA( srcdto, NULL, NULL, (Msg)(&gpl) );
  213.       }
  214.  
  215.       /* Get source datatype, see below */
  216.       (void)GetDTAttrs( srcdto, DTA_DataType, (&srcdtn), TAG_DONE );
  217.  
  218.       /* Create empty picture.datatype subclass object */
  219.       if( destdto = NewDTObject( NULL, DTA_SourceType,                   DTST_RAM,
  220.                                        XTAG( destdtn,    DTA_DataType ), destdtn,
  221.                                        XTAG( (!destdtn), DTA_GroupID  ), (srcdtn -> dtn_Header -> dth_GroupID),
  222.                                        TAG_DONE ) )
  223.       {
  224.         STRPTR                objname,
  225.                               objauthor,
  226.                               objannotation,
  227.                               objcopyright,
  228.                               objversion;
  229.         ULONG                 modeid;
  230.         ULONG                 numcolors;
  231.         struct BitMapHeader  *sbmh,
  232.                              *dbmh;
  233.         ULONG                *scregs;
  234.         struct ColorRegister *scm;
  235.  
  236.         if( GetDTAttrs( srcdto, DTA_ObjName,         (&objname),
  237.                                 DTA_ObjAuthor,       (&objauthor),
  238.                                 DTA_ObjAnnotation,   (&objannotation),
  239.                                 DTA_ObjCopyright,    (&objcopyright),
  240.                                 DTA_ObjVersion,      (&objversion),
  241.                                 PDTA_ModeID,         (&modeid),
  242.                                 PDTA_BitMapHeader,   (&sbmh),
  243.                                 PDTA_NumColors,      (&numcolors),
  244.                                 PDTA_CRegs,          (&scregs),
  245.                                 PDTA_ColorRegisters, (&scm),
  246.                                 PDTA_BitMap,         (&bm),
  247.                                 TAG_DONE ) == 11UL )
  248.         {
  249.           if( GetDTAttrs( destdto, PDTA_BitMapHeader, (&dbmh), TAG_DONE ) == 1UL )
  250.           {
  251.             if( sbmh && bm && dbmh )
  252.             {
  253.               ULONG                *dcregs;
  254.               struct ColorRegister *dcm;
  255.  
  256.               /* copy bitmapheader (RAW) from source bmh... */
  257.               *dbmh = *sbmh;
  258.  
  259.               /* ...and fill in attributes which may be different */
  260.               dbmh -> bmh_Compression = cmpNone;
  261.  
  262.               /* we're writing a full palette, set this flag (currently NOP, but...) */
  263.               dbmh -> bmh_Pad    = BMHDF_CMAPOK; /* also known as bmh_Flags */
  264.  
  265.               /* Set datas "name", "author", "annotation", "copyright", "version" etc.,
  266.                * screen mode id,
  267.                * our bitmap and
  268.                * the requested number of colors...
  269.                */
  270.               SetDTAttrs( destdto, NULL, NULL,
  271.                           DTA_ObjName,       objname,
  272.                           DTA_ObjAuthor,     objauthor,
  273.                           DTA_ObjAnnotation, objannotation,
  274.                           DTA_ObjCopyright,  objcopyright,
  275.                           DTA_ObjVersion,    objversion,
  276.                           PDTA_ModeID,       modeid,
  277.                           PDTA_NumColors,    numcolors,
  278.                           PDTA_BitMap,       bm,
  279.                           TAG_DONE );
  280.  
  281.               /* Some (ugly) picture.datatype needs a DTM_PROCLAYOUT before
  282.                * allowing to access their bitmaps
  283.                */
  284.               (void)GetDTAttrs( destdto, PDTA_BitMap, (&bm), TAG_DONE );
  285.  
  286.               if( bm == NULL )
  287.               {
  288.                 struct gpLayout  gpl;
  289.  
  290.                 Message( "destination requires layouting...\n" );
  291.  
  292.                 /* "Layout" picture (for those picture classes which need this,
  293.                  * __NOT__ recommened)
  294.                  */
  295.                 SetDTAttrs( destdto, NULL, NULL, PDTA_Remap, FALSE, TAG_DONE );
  296.  
  297.                 gpl . MethodID    = DTM_PROCLAYOUT;
  298.                 gpl . gpl_GInfo   = NULL;
  299.                 gpl . gpl_Initial = 1L;
  300.  
  301.                 DoDTMethodA( destdto, NULL, NULL, (Msg)(&gpl) );
  302.               }
  303.  
  304.               /* Get color tables (dest cregs, dest cm)  */
  305.               if( GetDTAttrs( destdto, PDTA_CRegs,          (&dcregs),
  306.                                        PDTA_ColorRegisters, (&dcm),
  307.                                        TAG_DONE ) == 2UL )
  308.               {
  309.                 /* Success ? */
  310.                 if( dcregs && dcm )
  311.                 {
  312.                   ULONG i;
  313.  
  314.                   /* Copy color tables... */
  315.                   for( i = 0UL ; i < numcolors ; i++ )
  316.                   {
  317.                     dcregs[ ((i * 3) + 0) ] = scregs[ ((i * 3) + 0) ]; /* copy r, */
  318.                     dcregs[ ((i * 3) + 1) ] = scregs[ ((i * 3) + 1) ]; /*      g, */
  319.                     dcregs[ ((i * 3) + 2) ] = scregs[ ((i * 3) + 2) ]; /*      b  */
  320.  
  321.                     *dcm++ = *scm++; /* copy colorregisters */
  322.                   }
  323.  
  324.                   Message( "saving picture: \"%s\"\n", destfile );
  325.  
  326.                   if( !(success = SaveDTObjectA( destdto, NULL, NULL, destfile, DTWM_RAW, TRUE, NULL )) )
  327.                   {
  328.                     Message( "saving failed\n" );
  329.                     error = IoErr();
  330.                   }
  331.                 }
  332.                 else
  333.                 {
  334.                   Message( "picture object mem err (no color tables)\n" );
  335.                   error = ERROR_NO_FREE_STORE;
  336.                 }
  337.               }
  338.               else
  339.               {
  340.                 /* can't get required args from object */
  341.                 error = ERROR_OBJECT_WRONG_TYPE;
  342.               }
  343.  
  344.               /* src datatype will free it's bitmap  */
  345.               SetDTAttrs( destdto, NULL, NULL, PDTA_BitMap, NULL, TAG_DONE );
  346.             }
  347.             else
  348.             {
  349.               Message( "can't get bitmap\n" );
  350.               error = ERROR_NO_FREE_STORE;
  351.             }
  352.           }
  353.           else
  354.           {
  355.             /* can't get required args from destination object */
  356.             Message( "can't get PDTA_BitMapHeader from destination object \n" );
  357.             error = ERROR_OBJECT_WRONG_TYPE;
  358.           }
  359.         }
  360.         else
  361.         {
  362.           /* can't get required args from source object */
  363.           Message( "can't get required args from source object\n" );
  364.           error = ERROR_OBJECT_WRONG_TYPE;
  365.         }
  366.  
  367.         /* Get rid of the picture object */
  368.         DisposeDTObject( destdto );
  369.       }
  370.       else
  371.       {
  372.         Message( "can't create empty object\n" );
  373.  
  374.         /* NewDTObject failed */
  375.         error = IoErr();
  376.       }
  377.     }
  378.     else
  379.     {
  380.       /* missing function args */
  381.       error = ERROR_REQUIRED_ARG_MISSING;
  382.     }
  383.  
  384.     SetIoErr( error );
  385.  
  386.     return( success );
  387. }
  388.  
  389.  
  390. static
  391. BOOL ConvertAnimation( Object *srcdto, struct DataType *destdtn, STRPTR destfile )
  392. {
  393.     BOOL success = FALSE;
  394.     LONG error   = 0L;
  395.  
  396.     Message( "animation converter\n" );
  397.  
  398.     if( srcdto && destfile && destdtn )
  399.     {
  400.       TEXT             classname[ 256 ];
  401.       struct Library  *DTClassBase;
  402.       Object          *destdto;
  403.       struct DataType *srcdtn;
  404.  
  405.       /* Get source datatype, see below */
  406.       (void)GetDTAttrs( srcdto, DTA_DataType, (&srcdtn), TAG_DONE );
  407.  
  408.       /* Create class library name... */
  409.       mysprintf( classname, "DataTypes/%s.datatype", (destdtn -> dtn_Header -> dth_BaseName) );
  410.  
  411.       /* ...then open it */
  412.       if( DTClassBase = OpenLibrary( classname, 0UL ) )
  413.       {
  414.         struct IClass *AnimClass;
  415.  
  416.         /* Obtain destination class */
  417.         if( AnimClass = ObtainEngine() )
  418.         {
  419.           struct IClass *WriteAnimClass;
  420.  
  421.           /* Create our new class... */
  422.           if( WriteAnimClass = MakeClass( NULL, NULL, AnimClass, 0UL, 0UL ) )
  423.           {
  424.             /* Prepare class for usage... */
  425.             WriteAnimClass -> cl_Dispatcher . h_Entry = (HOOKFUNC)WriteAnimAndSoundDispatch;
  426.             WriteAnimClass -> cl_UserData             = 0UL; /* datatypes.library expects here a (struct Library *) or NULL,
  427.                                                               * therefore we cannot use this for our context data
  428.                                                               */
  429.  
  430.             /* Create empty subclass object (from our dest write class) */
  431.             if( destdto = (Object *)NewDTObject( NULL, DTA_SourceType,          DTST_RAM,       /* Create empty object...    */
  432.                                                        DTA_DataType,            destdtn,        /* ...using this datatype... */
  433.                                                        DTA_Class,               WriteAnimClass, /* ...from this class...     */
  434.                                                        WRITEANIMA_SourceObject, srcdto,         /* source of data            */
  435.                                                        TAG_DONE ) )
  436.             {
  437.               /* datatypesclass attributes */
  438.               STRPTR                objname,
  439.                                     objauthor,
  440.                                     objannotation,
  441.                                     objcopyright,
  442.                                     objversion;
  443.  
  444.               /* picture (e.g. global) attributes  */
  445.               ULONG                 modeid;
  446.               ULONG                 numcolors;
  447.               struct BitMap        *bm;
  448.               struct BitMapHeader  *sbmh,
  449.                                    *dbmh;
  450.               ULONG                *scregs;
  451.               struct ColorRegister *scm;
  452.               ULONG                 width,
  453.                                     height,
  454.                                     depth;
  455.                                     
  456.               /* animation attributes */
  457.               ULONG                 frames,
  458.                                     fps,
  459.                                     finc;
  460.                                     
  461.               /* sound attributes */
  462.               BYTE                 *sample;
  463.               ULONG                 samplelength,
  464.                                     period,
  465.                                     volume,
  466.                                     cycles;
  467.  
  468.               if( GetDTAttrs( srcdto, DTA_ObjName,          (&objname),
  469.                                       DTA_ObjAuthor,        (&objauthor),
  470.                                       DTA_ObjAnnotation,    (&objannotation),
  471.                                       DTA_ObjCopyright,     (&objcopyright),
  472.                                       DTA_ObjVersion,       (&objversion),
  473.                                       PDTA_BitMapHeader,    (&sbmh),
  474.                                       ADTA_ModeID,          (&modeid),
  475.                                       ADTA_NumColors,       (&numcolors),
  476.                                       ADTA_CRegs,           (&scregs),
  477.                                       ADTA_ColorRegisters,  (&scm),
  478.                                       ADTA_KeyFrame,        (&bm),
  479.                                       ADTA_Width,           (&width),
  480.                                       ADTA_Height,          (&height),
  481.                                       ADTA_Depth,           (&depth),
  482.                                       ADTA_Frames,          (&frames),
  483.                                       ADTA_FramesPerSecond, (&fps),
  484.                                       ADTA_FrameIncrement,  (&finc),
  485.                                       ADTA_Sample,          (&sample),
  486.                                       ADTA_SampleLength,    (&samplelength),
  487.                                       ADTA_Period,          (&period),
  488.                                       ADTA_Volume,          (&volume),
  489.                                       ADTA_Cycles,          (&cycles),
  490.                                       TAG_DONE ) == 22UL )
  491.               {
  492.                 if( GetDTAttrs( destdto, PDTA_BitMapHeader, (&dbmh), TAG_DONE ) == 1UL )
  493.                 {
  494.                   if( sbmh && bm && dbmh )
  495.                   {
  496.                     ULONG                *dcregs;
  497.                     struct ColorRegister *dcm;
  498.  
  499.                     /* copy bitmapheader (RAW) from animation bmh... */
  500.                     *dbmh = *sbmh;
  501.  
  502.                     /* ...and fill in attributes which may be different */
  503.                     dbmh -> bmh_Compression = cmpNone;
  504.  
  505.                     /* we're writing a full palette, set this flag (currently NOP, but...) */
  506.                     dbmh -> bmh_Pad  = BMHDF_CMAPOK; /* also known as bmh_Flags */
  507.  
  508.                     /* Set datas "name", "author", "annotation", "copyright", "version" etc.,
  509.                      * screen mode id,
  510.                      * our bitmap and
  511.                      * the requested number of colors...
  512.                      */
  513.                     SetDTAttrs( destdto, NULL, NULL,
  514.                                 DTA_ObjName,          objname,
  515.                                 DTA_ObjAuthor,        objauthor,
  516.                                 DTA_ObjAnnotation,    objannotation,
  517.                                 DTA_ObjCopyright,     objcopyright,
  518.                                 DTA_ObjVersion,       objversion,
  519.                                 PDTA_BitMapHeader,    sbmh,
  520.                                 ADTA_ModeID,          modeid,
  521.                                 ADTA_NumColors,       numcolors,
  522.                                 ADTA_CRegs,           scregs,
  523.                                 ADTA_ColorRegisters,  scm,
  524.                                 ADTA_KeyFrame,        bm,
  525.                                 ADTA_Width,           width,
  526.                                 ADTA_Height,          height,
  527.                                 ADTA_Depth,           depth,
  528.                                 ADTA_Frames,          frames,
  529.                                 ADTA_FramesPerSecond, fps,
  530.                                 ADTA_FrameIncrement,  finc,
  531.                                 ADTA_Sample,          sample,
  532.                                 ADTA_SampleLength,    samplelength,
  533.                                 ADTA_Period,          period,
  534.                                 ADTA_Volume,          volume,
  535.                                 ADTA_Cycles,          cycles,
  536.                                 TAG_DONE );
  537.  
  538.                     /* Get color tables (dest cregs, dest cm)  */
  539.                     if( GetDTAttrs( destdto, ADTA_CRegs,          (&dcregs),
  540.                                              ADTA_ColorRegisters, (&dcm),
  541.                                              TAG_DONE ) == 2UL )
  542.                     {
  543.                       /* Success ? */
  544.                       if( (dcregs && dcm) || (numcolors == 0UL) )
  545.                       {
  546.                         ULONG i;
  547.  
  548.                         /* Copy color tables... */
  549.                         for( i = 0UL ; i < numcolors ; i++ )
  550.                         {
  551.                           dcregs[ ((i * 3) + 0) ] = scregs[ ((i * 3) + 0) ]; /* copy r, */
  552.                           dcregs[ ((i * 3) + 1) ] = scregs[ ((i * 3) + 1) ]; /*      g, */
  553.                           dcregs[ ((i * 3) + 2) ] = scregs[ ((i * 3) + 2) ]; /*      b  */
  554.  
  555.                           *dcm++ = *scm++; /* copy colorregisters */
  556.                         }
  557.  
  558.                         Message( "saving animation: \"%s\"\n", destfile );
  559.  
  560.                         if( !(success = SaveDTObjectA( destdto, NULL, NULL, destfile, DTWM_RAW, TRUE, NULL )) )
  561.                         {
  562.                           Message( "saving failed\n" );
  563.                           error = IoErr();
  564.                         }
  565.                       }
  566.                       else
  567.                       {
  568.                         Message( "animation object mem err (no color tables)\n" );
  569.                         error = ERROR_NO_FREE_STORE;
  570.                       }
  571.                     }
  572.                     else
  573.                     {
  574.                       /* can't get required args from object */
  575.                       error = ERROR_OBJECT_WRONG_TYPE;
  576.                     }
  577.  
  578.                     /* src datatype will free it's key frame (bitmap) */
  579.                     SetDTAttrs( destdto, NULL, NULL, ADTA_KeyFrame, NULL, TAG_DONE );
  580.                   }
  581.                   else
  582.                   {
  583.                     Message( "can't get key frame (bitmap)\n" );
  584.                     error = ERROR_NO_FREE_STORE;
  585.                   }
  586.                 }
  587.                 else
  588.                 {
  589.                   /* can't get required args from destination object */
  590.                   Message( "can't get PDTA_BitMapHeader from destination\n" );
  591.                   error = ERROR_OBJECT_WRONG_TYPE;
  592.                 }
  593.               }
  594.               else
  595.               {
  596.                 /* can't get required args from source object */
  597.                 Message( "can't get required args from source object\n" );
  598.                 error = ERROR_OBJECT_WRONG_TYPE;
  599.               }
  600.  
  601.               /* Get rid of the animation object */
  602.               DisposeDTObject( destdto );
  603.             }
  604.             else
  605.             {
  606.               Message( "can't create empty object\n" );
  607.  
  608.               /* NewDTObject failed */
  609.               error = IoErr();
  610.             }
  611.  
  612.             /* Free class... */
  613.             while( !FreeClass( WriteAnimClass ) )
  614.             {
  615.               /* Let other tasks partake... */
  616.               Delay( (TICKS_PER_SECOND / 5UL) );
  617.             }
  618.           }
  619.         }
  620.         else
  621.         {
  622.           Message( "can't obtain output anim class\n" );
  623.         }
  624.  
  625.         CloseLibrary( DTClassBase );
  626.       }
  627.       else
  628.       {
  629.         /* Can't open class library */
  630.         if( !(error = IoErr()) )
  631.         {
  632.           error = DTERROR_UNKNOWN_DATATYPE;
  633.         }
  634.       }
  635.     }
  636.     else
  637.     {
  638.       /* missing function args */
  639.       error = ERROR_REQUIRED_ARG_MISSING;
  640.     }
  641.  
  642.     SetIoErr( error );
  643.  
  644.     return( success );
  645. }
  646.  
  647.  
  648. static
  649. BOOL ConvertSound( Object *srcdto, struct DataType *destdtn, STRPTR destfile )
  650. {
  651.     BOOL success = FALSE;
  652.     LONG error   = 0L;
  653.  
  654.     Message( "sound converter\n" );
  655.  
  656.     if( srcdto && destfile )
  657.     {
  658.       Object          *destdto;
  659.       struct DataType *srcdtn;
  660.  
  661.       /* Get source datatype, see below */
  662.       (void)GetDTAttrs( srcdto, DTA_DataType, (&srcdtn), TAG_DONE );
  663.  
  664.       /* Create empty sound.datatype subclass object */
  665.       if( destdto = NewDTObject( NULL, DTA_SourceType,                   DTST_RAM,
  666.                                        XTAG( destdtn,    DTA_DataType ), destdtn,
  667.                                        XTAG( (!destdtn), DTA_GroupID  ), (srcdtn -> dtn_Header -> dth_GroupID),
  668.                                        TAG_DONE ) )
  669.       {
  670.         STRPTR                objname,
  671.                               objauthor,
  672.                               objannotation,
  673.                               objcopyright,
  674.                               objversion;
  675.         struct VoiceHeader   *svh,
  676.                              *dvh;
  677.         SAMPLE8              *sample;
  678.         ULONG                 samplelength;
  679.         ULONG                 period,
  680.                               volume,
  681.                               cycles;
  682.  
  683.         if( GetDTAttrs( srcdto, DTA_ObjName,         (&objname),
  684.                                 DTA_ObjAuthor,       (&objauthor),
  685.                                 DTA_ObjAnnotation,   (&objannotation),
  686.                                 DTA_ObjCopyright,    (&objcopyright),
  687.                                 DTA_ObjVersion,      (&objversion),
  688.                                 SDTA_VoiceHeader,    (&svh),
  689.                                 SDTA_Sample,         (&sample),
  690.                                 SDTA_SampleLength,   (&samplelength),
  691.                                 SDTA_Period,         (&period),
  692.                                 SDTA_Volume,         (&volume),
  693.                                 SDTA_Cycles,         (&cycles),
  694.                                 TAG_DONE ) == 11UL )
  695.         {
  696.           if( GetDTAttrs( destdto, SDTA_VoiceHeader, (&dvh), TAG_DONE ) == 1UL )
  697.           {
  698.             if( svh && sample && samplelength && dvh )
  699.             {
  700.               /* copy voiceheader (RAW) from source vh... */
  701.               *dvh = *svh;
  702.  
  703.               /* ...and fill in attributes which may be different */
  704.               dvh -> vh_Compression = CMP_NONE;
  705.  
  706.               /* Set datas "name", "author", "annotation", "copyright", "version" etc.,
  707.                * screen mode id,
  708.                * our sample data etc.
  709.                */
  710.               SetDTAttrs( destdto, NULL, NULL,
  711.                           DTA_ObjName,       objname,
  712.                           DTA_ObjAuthor,     objauthor,
  713.                           DTA_ObjAnnotation, objannotation,
  714.                           DTA_ObjCopyright,  objcopyright,
  715.                           DTA_ObjVersion,    objversion,
  716.                           SDTA_Sample,       sample,
  717.                           SDTA_SampleLength, samplelength,
  718.                           SDTA_Period,       period,
  719.                           SDTA_Volume,       volume,
  720.                           SDTA_Cycles,       cycles,
  721.                           TAG_DONE );
  722.  
  723.               Message( "saving sound: \"%s\"\n", destfile );
  724.  
  725.               if( !(success = SaveDTObjectA( destdto, NULL, NULL, destfile, DTWM_RAW, TRUE, NULL )) )
  726.               {
  727.                 Message( "saving failed\n" );
  728.                 error = IoErr();
  729.               }
  730.  
  731.               /* src datatype will free it's sample data  */
  732.               SetDTAttrs( destdto, NULL, NULL, SDTA_Sample, NULL, TAG_DONE );
  733.             }
  734.             else
  735.             {
  736.               Message( "can't get sample (maybe unsupported sound.datatype V41 mode)\n" );
  737.               error = ERROR_NO_FREE_STORE;
  738.             }
  739.           }
  740.           else
  741.           {
  742.             /* can't get required args from destination object */
  743.             Message( "can't get required args from destination object\n" );
  744.             error = ERROR_OBJECT_WRONG_TYPE;
  745.           }
  746.         }
  747.         else
  748.         {
  749.           /* can't get required args from source object */
  750.           Message( "can't get required args from source object\n" );
  751.           error = ERROR_OBJECT_WRONG_TYPE;
  752.         }
  753.  
  754.         /* Get rid of the sound object */
  755.         DisposeDTObject( destdto );
  756.       }
  757.       else
  758.       {
  759.         Message( "can't create empty object\n" );
  760.  
  761.         /* NewDTObject failed */
  762.         error = IoErr();
  763.       }
  764.     }
  765.     else
  766.     {
  767.       /* missing function args */
  768.       error = ERROR_REQUIRED_ARG_MISSING;
  769.     }
  770.  
  771.     SetIoErr( error );
  772.  
  773.     return( success );
  774. }
  775.  
  776.  
  777. /* WriteAnimClass/WriteSoundClass dispatcher */
  778. static
  779. DISPATCHERFLAGS
  780. ULONG WriteAnimAndSoundDispatch( REGA0 struct IClass *cl, REGA2 Object *o, REGA1 Msg msg )
  781. {
  782.     ULONG retval = 0UL;
  783.  
  784.     switch( msg -> MethodID )
  785.     {
  786. #ifdef CURRENTLY_USELESS_CODE
  787.         case OM_NEW:
  788.         {
  789.             /* Assumes that WRITEANIMA_SourceObject contains a valid object
  790.              * (WRITEANIMA_SourceObject s mapped to DTA_UserData; datatypesclass handles
  791.              * storage of this attribute :-)
  792.              */
  793.             if( retval = DoSuperMethodA( cl, o, msg ) )
  794.             {
  795.               /* NOP */
  796.             }
  797.         }
  798.             break;
  799.  
  800.         case OM_DISPOSE:
  801.         {
  802.             DoSuperMethodA( cl, o, msg );
  803.         }
  804.             break;
  805. #endif /* CURRENTLY_USELESS_CODE */
  806.  
  807.         case OM_UPDATE:
  808.         {
  809.             /* Avoid OM_NOTIFY loops */
  810.             if( DoMethod( o, ICM_CHECKLOOP ) )
  811.             {
  812.               break;
  813.             }
  814.         }
  815.         case OM_SET:
  816.         {
  817.             /* Pass the attributes to the text class and force a refresh
  818.              * if we need it
  819.              */
  820.             if( retval = DoSuperMethodA( cl, o, msg ) )
  821.             {
  822. /* writeanimclass is alwasy the top instance... */
  823. #ifdef COMMENTED_OUT
  824.               /* Top instance ? */
  825.               if( OCLASS( o ) == cl )
  826. #endif /* COMMENTED_OUT */
  827.               {
  828.                 struct RastPort *rp;
  829.  
  830.                 /* Get a pointer to the rastport */
  831.                 if( rp = ObtainGIRPort( (((struct opSet *)msg) -> ops_GInfo) ) )
  832.                 {
  833.                   struct gpRender gpr;
  834.  
  835.                   /* Force a redraw */
  836.                   gpr . MethodID   = GM_RENDER;
  837.                   gpr . gpr_GInfo  = ((struct opSet *)msg) -> ops_GInfo;
  838.                   gpr . gpr_RPort  = rp;
  839.                   gpr . gpr_Redraw = GREDRAW_UPDATE;
  840.  
  841.                   DoMethodA( o, (Msg)(&gpr) );
  842.  
  843.                   /* Release the temporary rastport */
  844.                   ReleaseGIRPort( rp );
  845.                 }
  846.  
  847.                 retval = 0UL;
  848.               }
  849.             }
  850.         }
  851.             break;
  852.  
  853.         /* This two methods are superset by writeanimclass (e.g. us) */
  854.         case ADTM_LOADFRAME:
  855.         case ADTM_UNLOADFRAME:
  856.         {
  857.             Object *source;
  858.  
  859.             if( GetDTAttrs( o, WRITEANIMA_SourceObject, (&source), TAG_DONE ) == 1UL )
  860.             {
  861.               if( source )
  862.               {
  863.                 /* Route msg to source object... */
  864.                 retval = DoMethodA( source, msg );
  865.               }
  866.               else
  867.               {
  868.                 /* This error code aborts animation.datatype's playback and should also abort any encoder
  869.                  * (only ERROR_OBJECT_NOT_FOUND should be ignored by encoders...)
  870.                  */
  871.                 SetIoErr( ERROR_REQUIRED_ARG_MISSING );
  872.               }
  873.             }
  874.         }
  875.             break;
  876.  
  877. #ifdef SOUNDDATATYPE_V41_SUPPORT
  878.         /* This two methods are superset by writesoundclass (e.g. us) */
  879.         case SDTM_LOADSAMPLE:
  880.         case SDTM_UNLOADSAMPLE:
  881.         {
  882.             Object *source;
  883.  
  884.             if( GetDTAttrs( o, WRITESOUNDA_SourceObject, (&source), TAG_DONE ) )
  885.             {
  886.               if( source )
  887.               {
  888.                 /* Route msg to source object... */
  889.                 retval = DoMethodA( source, msg );
  890.               }
  891.               else
  892.               {
  893.                 /* This error code aborts animation.datatype's playback and should also abort any encoder
  894.                  * (only ERROR_OBJECT_NOT_FOUND should be ignored by encoders...)
  895.                  */
  896.                 SetIoErr( ERROR_REQUIRED_ARG_MISSING );
  897.               }
  898.             }
  899.         }
  900.             break;
  901. #endif /* SOUNDDATATYPE_V41_SUPPORT */
  902.  
  903.         /* Let the superclass handle everything else */
  904.         default:
  905.         {
  906.             retval = DoSuperMethodA( cl, o, msg );
  907.         }
  908.             break;
  909.     }
  910.  
  911.     return( retval );
  912. }
  913.  
  914.  
  915.  
  916.  
  917.